package cn.t.web.device;

import cn.t.core.exception.BizException;
import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.enums.order.ErrorEnum;
import cn.t.facade.device.DeviceFacade;
import cn.t.listener.CustomCellWriteWidthConfig;
import cn.t.request.InventoryReq;
import cn.t.utils.ExcelUtil;
import cn.t.vo.device.InventoryCargowayExcelVO;
import cn.t.vo.device.InventoryCargowayVO;
import cn.t.vo.device.InventoryDeviceExcelVO;
import cn.t.vo.device.InventoryDeviceVO;
import cn.t.vo.device.InventorySkuExcelVO;
import cn.t.vo.device.InventorySkuVO;
import com.alibaba.excel.EasyExcel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 库存管理
 *
 * @author s
 * @since 2024-07-16
 */
@Slf4j
@RestController
@RequestMapping("/inventory")
// "库存服务")
@AllArgsConstructor
public class InventoryController {

    private final DeviceFacade deviceFacade;

    /**
     * 库存管理-按设备
     */
    @PostMapping("/findDevicePage")
    public PageInfoDTO<InventoryDeviceVO> findInventoryDevicePage(@RequestBody PageQueryDTO<InventoryReq> req) {
        return deviceFacade.findInventoryDevicePage(req);
    }

    /**
     * 库存管理-按设备导出
     */
    @PostMapping("/export/device")
    public void exportInventoryDevice(HttpServletResponse response, @RequestBody InventoryReq req) {
        List<InventoryDeviceExcelVO> inventoryDeviceExcelVOS = deviceFacade.exportInventoryDevice(req);
        try {
            ExcelUtil.setHeader(response, URLEncoder.encode("库存管理-按设备", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            EasyExcel
                    .write(outputStream, InventoryDeviceExcelVO.class)
                    .sheet("库存管理-按设备")
                    .registerWriteHandler(new CustomCellWriteWidthConfig())
                    .doWrite(inventoryDeviceExcelVOS);
        } catch (Exception e) {
            throw new BizException(ErrorEnum.NO_DATA, String.format(ErrorEnum.NO_DATA.getMessage()));
        }
    }

    /**
     * 库存管理-按商品
     */
    @PostMapping("/findSkuPage")
    public PageInfoDTO<InventorySkuVO> findInventorySkuPage(@RequestBody PageQueryDTO<InventoryReq> req) {
        return deviceFacade.findInventorySkuPage(req);
    }

    /**
     * 库存管理-按商品导出
     */
    @PostMapping("/export/sku")
    public void exportInventorySku(HttpServletResponse response, @RequestBody InventoryReq req) {
        List<InventorySkuExcelVO> inventoryDeviceExcelVOS = deviceFacade.exportInventorySku(req);
        try {
            ExcelUtil.setHeader(response, URLEncoder.encode("库存管理-按商品", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            EasyExcel
                    .write(outputStream, InventorySkuExcelVO.class)
                    .sheet("库存管理-按商品")
                    .registerWriteHandler(new CustomCellWriteWidthConfig())
                    .doWrite(inventoryDeviceExcelVOS);
        } catch (Exception e) {
            throw new BizException(ErrorEnum.NO_DATA, String.format(ErrorEnum.NO_DATA.getMessage()));
        }
    }

    /**
     * 库存管理-按货道
     */
    @PostMapping("/findCargowayPage")
    public PageInfoDTO<InventoryCargowayVO> findCargowayPage(@RequestBody PageQueryDTO<InventoryReq> req) {
        return deviceFacade.findCargowayPage(req);
    }

    /**
     * 库存管理-按货道导出
     */
    @PostMapping("/export/cargoway")
    public void exportInventoryCargoway(HttpServletResponse response, @RequestBody InventoryReq req) {
        List<InventoryCargowayExcelVO> inventoryCargowayExcelVOS = deviceFacade.exportInventoryCargoway(req);
        try {
            ExcelUtil.setHeader(response, URLEncoder.encode("库存管理-按货道", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            EasyExcel
                    .write(outputStream, InventoryCargowayExcelVO.class)
                    .sheet("库存管理-按货道")
                    .registerWriteHandler(new CustomCellWriteWidthConfig())
                    .doWrite(inventoryCargowayExcelVOS);
        } catch (Exception e) {
            throw new BizException(ErrorEnum.NO_DATA, String.format(ErrorEnum.NO_DATA.getMessage()));
        }
    }

}

